//
//  QYGTabBar.m
//  wawaji_ios
//
//  Created by 奇艺果 on 2017/12/26.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGTabBar.h"

@interface QYGTabBar()

@property (nonatomic, strong) UIButton *middleView;
@property (nonatomic, strong) UIImageView *bgImgView;
@end

@implementation QYGTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        [self setUpInit];
    }
    return self;
}

- (void)setUpInit {
    
    [[UITabBar appearance] setBackgroundImage:[UIImage new]];
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBarItem appearance]setTitleTextAttributes:@{NSFontAttributeName:ScaleFont(10)}   forState:UIControlStateNormal];
    [[UITabBarItem appearance]setTitleTextAttributes:@{NSFontAttributeName:ScaleFont(10)} forState:UIControlStateSelected];
    [self addSubview:self.middleView];
    CGFloat width = ScaleW(39);
    CGFloat height = ScaleW(25);
    self.middleView.frame = CGRectMake(0, 0, width, height);
    self.tintColor = [UIColor qyg_mainColor];
    [self insertSubview:self.bgImgView atIndex:0];
}
 + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
     if (!color || size.width <=0 || size.height <=0) return nil;
     CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
     UIGraphicsBeginImageContextWithOptions(rect.size,NO, 0);
     CGContextRef context =UIGraphicsGetCurrentContext();
     CGContextSetFillColorWithColor(context, color.CGColor);
     CGContextFillRect(context, rect);
     UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return image;
 }

-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.middleView.centerX = self.frame.size.width * 0.5;
    self.middleView.centerY = self.frame.size.height * 0.5 - ScaleW(7) - SafeBottom * 0.5;
//    self.middleView.y = self.height - self.middleView.height  -SafeBottom;
    self.bgImgView.frame = self.bounds;
}

- (UIButton *)middleView{
    if(!_middleView){
        _middleView = [[UIButton alloc] init];
        [_middleView setBackgroundImage:[UIImage imageNamed:@"main_rech_icon"] forState:UIControlStateNormal];
        [_middleView addTarget:self action:@selector(middleViewClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _middleView;
}

- (void)middleViewClicked{
    
    if(self.middleClickBlock){
        self.middleClickBlock();
    }
}
- (UIImageView *)bgImgView{
    if(!_bgImgView){
        
        _bgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_bg"]];
        _bgImgView.userInteractionEnabled = YES;
    }
    return _bgImgView;
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    
    // 设置允许交互的区域
    CGPoint pointInMiddleBtn = [self convertPoint:point toView:self.middleView];
    
    CGPoint middleBtnCenter = CGPointMake(33, 33);
    CGFloat distance = sqrt(pow(pointInMiddleBtn.x - middleBtnCenter.x, 2) + pow(pointInMiddleBtn.y - middleBtnCenter.y, 2));
    
    if (pointInMiddleBtn.y < 3.5 || (distance > 33 && pointInMiddleBtn.y < 18)) {
        return NO;
    }
    
    return YES;
}

@end
